package imoney.shared.model;


import imoney.shared.exception.AlreadyExistingException;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;



/**
 * The aim of this abstract class is to provide a generic pattern that allows to check, prepare and save an object in the database,
 * regardless of its attributes. This class ensures that any object extending it will be provided with Parent Key fields,
 * that are necessary to create entities (which are necessary to use transactions). Thus, any PersistenceCapable object should
 * extend this class, especially objects that will interact during transactions.
 * @author Arnaud PELOQUIN
 *
 */

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)

public abstract class Registreable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    @Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
    private Key parentKey;
	@Persistent
	private int id=-10;
    
    
	public Key getKey(){
		return key;
	}
	public Key getParentKey(){
		return parentKey;
	}
	public int getId(){
		return id;
	}
	
	public void setKey(Key cle){
		key = cle;
	}
	public void setParentKey(Key cle){
		parentKey = cle;
	}
	public void setId(int n){
		id = n;
	}

	public String toString()
	{
		return "ID : "+id;
	}
	/**
	 * This method should help throw an AlreadyExistingException if specified field values of the object are already used by
	 * another object. For example the 'name' field for the class Person has to be unique, so the corresponding
	 * query for person should include a condition over the name.
	 * @param pm: the PersistenceManager that will give access to the datastore to look for a twin
	 * @throws AlreadyExistingException
	 */
	public abstract String getAvailabilityQuery();
	
	/**
	 * 
	 * @return the String that will be displayed in case of a AlreadyExistingExecption for this object
	 */
	public abstract String getAvailabilityExceptionMessage();
}
